/* 
*  Copyright (C) 2009-2010 WCG Project
*
*  This program is free software; you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by the
*  Free Software Foundation; either version 2 of the License, or (at
*  your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but
*  WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software Foundation,
*  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*  In addition, as a special exception, the author gives permission to
*  link the code of this program with the Half-Life Game Engine ("HL
*  Engine") and Modified Game Libraries ("MODs") developed by Valve,
*  L.L.C ("Valve"). You must obey the GNU General Public License in all
*  respects for all of the code used other than the HL Engine and MODs
*  from Valve. If you modify this file, you may extend this exception
*  to your version of the file, but you are not obligated to do so. If
*  you do not wish to do so, delete this exception statement from your
*  version.
*/



/*
*	Shared Functions
*/

new g_iSpawnReserved[64]				// Used to make sure a spawn point isn't used for another player
new g_iSpawnInc = 0

new g_iObjectiveEnt[MAX_OBJECTIVES]
new g_iObjectiveEntType[MAX_OBJECTIVES]
new Float:g_fObjectiveOrigin[MAX_OBJECTIVES][3]
new g_iTotalObjectiveEnts = 0



//-------------------------------------------
// SHARED_Init
//
//-------------------------------------------
SHARED_Init()
{
	// We need to determine the objective entities (if they exist)
	new i, bool:bFound

	for ( i = 0; i <= global_get(glb_maxEntities) ; i++ )
	{
		if ( !pev_valid( i ) )
			continue
		
		// More than were necessary was found
		if( g_iTotalObjectiveEnts >= MAX_OBJECTIVES )
		{
			WC3_Log( true, "Woa we found more than 11" )
			break
		}

		bFound = false

		static szClassName[64]
		pev( i, pev_classname, szClassName, 63 )

		// VIP Escape zone!!
		if( equal( szClassName, "func_vip_safetyzone") )	
		{
			g_iObjectiveEntType[g_iTotalObjectiveEnts] = OBJENT_VIP_ESCAPE
			bFound = true
		}

		// Hostage zone
		else if ( equal( szClassName, "hostage_entity") )
		{
			g_iObjectiveEntType[g_iTotalObjectiveEnts] = OBJENT_HOSTAGE
			bFound = true
		}

		// Bomb zone
		else if ( equal( szClassName, "func_bomb_target") )
		{
			g_iObjectiveEntType[g_iTotalObjectiveEnts] = OBJENT_BOMBSITE
			bFound = true
		}

		// Hostage escape zone
		else if ( equal( szClassName, "func_escapezone") )
		{
			g_iObjectiveEntType[g_iTotalObjectiveEnts] = OBJENT_HOSTAGE_ESCAPE
			bFound = true
		}

		// We found an objective entity!!!
		if ( bFound )
		{
			g_iObjectiveEnt[g_iTotalObjectiveEnts] = i

			fm_get_brush_entity_origin( i, g_fObjectiveOrigin[g_iTotalObjectiveEnts] )

			// Increment the total number
			g_iTotalObjectiveEnts++
		}
	}
	
}









//-------------------------------------------
// SHARED_SaveWeapons
//
//-------------------------------------------
public SHARED_SaveWeapons( id )
{
	if( !is_user_alive(id) )
		return

	// Clear Array
	for( new i = 0; i < 32; i++ )
		g_PlayerWeapons[id][i] = 0

	new num = 0
	get_user_weapons( id, g_PlayerWeapons[id], num )
}



//-------------------------------------------
// SHARED_CopySavedWeapons
//
// Copy the weapons over right before reset hud is called, that way we don't lose our last round weapons when the round restarts
//-------------------------------------------
public SHARED_CopySavedWeapons( id )
{
	for ( new i = 0; i < 33; i++ )
	{
		for ( new j = 0; j < 32; j++ )
			g_PlayerLastWeapons[i][j] = g_PlayerWeapons[i][j]
	}
}









//-------------------------------------------
// SHARED_ResetMaxSpeed
//
//-------------------------------------------
public SHARED_ResetMaxSpeed( id )
{
	id = GetTaskID( id, TASK_RESETSPEED )

	if( !is_user_connected(id) )
		return

	// User should no longer be stunned
	PlayerInfo[id][PI_IsStunned] = false

	// User should no longer be slowed
	PlayerInfo[id][PI_Slowed] = 0

	// User should no longer be immobilized
	PlayerInfo[id][PI_IsImmobilized] = false

	PLAYER_UpdateSpeed( id )
	PLAYER_UpdateGravity( id )
}




//-------------------------------------------
// SHARED_IsPlayerSlowed
//
// Returns true if the user is hexed/stunned/slowed
//-------------------------------------------
SHARED_IsPlayerSlowed( id )
{
	if( PlayerInfo[id][PI_IsStunned] || PlayerInfo[id][PI_Slowed] || PlayerInfo[id][PI_IsImmobilized] )
		return true

	return false
}



//-------------------------------------------
// _SHARED_SpawnReset
//
// Reset our reserved spawn points
//-------------------------------------------
public _SHARED_SpawnReset()
{
	new i
	new num = sizeof(g_iSpawnReserved)
	
	g_iSpawnInc = 0
	
	for ( i = 0; i < num; i++ )
		g_iSpawnReserved[i] = 0
}



//-------------------------------------------
// SHARED_SpawnReserved
//
// Determine if this entity is reserved for another player
//-------------------------------------------
bool:SHARED_SpawnReserved( ent )
{
	for( new i = 0; i < g_iSpawnInc; i++ )
	{
		if ( g_iSpawnReserved[i] == ent )
			return true
	}

	return false
}






#define MAXGLOW					150

//-------------------------------------------
// SHARED_Glow
//
//-------------------------------------------
SHARED_Glow( id, RGB_Color )
{
	static iRed, iGreen, iBlue

	// Not allowed to glow right now...
	if ( !PlayerInfo[id][PI_CanRender] )
		return
		
	// Don't glow if invisible
	else if ( SM_GetSkillLevel( id, SKILL_INVISIBILITY ) > 0 || ITEM_Has( id, ITEM_CLOAK ) > ITEM_NONE )
		return

	iRed = min( COLOR_RED( PlayerInfo[id][PI_GlowLevel] ) + COLOR_RED( RGB_Color ), MAXGLOW )
	iGreen = min( COLOR_GREEN( PlayerInfo[id][PI_GlowLevel] ) + COLOR_GREEN( RGB_Color ), MAXGLOW )
	iBlue = min( COLOR_BLUE( PlayerInfo[id][PI_GlowLevel] ) + COLOR_BLUE( RGB_Color ), MAXGLOW )

	// Store the colors in playerinfo
	PlayerInfo[id][PI_GlowLevel] = COLOR_RGB( iRed, iGreen, iBlue )
		
	_SHARED_Glow( id )
}



//-------------------------------------------
// _SHARED_Glow
//
//-------------------------------------------
public _SHARED_Glow( id )
{
	static iRed, iGreen, iBlue
	
	id = GetTaskID( id, TASK_GLOW )

	// User is no longer connected, so lets not continue this!
	if ( !is_user_connected(id) || !PlayerInfo[id][PI_CanRender] )
		return

	iRed	= COLOR_RED( PlayerInfo[id][PI_GlowLevel] )
	iGreen	= COLOR_GREEN( PlayerInfo[id][PI_GlowLevel] )
	iBlue	= COLOR_BLUE( PlayerInfo[id][PI_GlowLevel] )

	if( iRed || iGreen || iBlue )
	{
		iRed = max( iRed - 5, 0 )
		iGreen = max( iGreen - 5, 0 )
		iBlue = max( iBlue - 5, 0 )
		
		PlayerInfo[id][PI_GlowLevel] = COLOR_RGB( iRed, iGreen, iBlue )

		fm_set_user_rendering( id, kRenderFxGlowShell, iRed, iGreen, iBlue, kRenderNormal, 16 )
	}

	// No more glowing!
	else
	{
		fm_set_user_rendering( id )
		return
	}

	set_task( 0.2, "_SHARED_Glow", TASK_GLOW + id )
}




//-------------------------------------------
// SHARED_NearObjective
//
// Returns if an origin is near an objective (returns which objective)
//-------------------------------------------
SHARED_NearObjective( vOrigin[3] )
{
	new i, Float:fOrigin[3]
	
	// Convert vector to float
	IVecFVec( vOrigin, fOrigin )

	// Check the distances
	for( i = 0; i < g_iTotalObjectiveEnts; i++ )
	{
		new Float:fDistance = vector_distance( fOrigin, g_fObjectiveOrigin[i] )

		if( fDistance < 250.0 )
			return g_iObjectiveEntType[i]
	}

	return -1
}




//-------------------------------------------
// SHARED_Teleport
//
// This will teleport a user to a location and test to make sure they were actually moved there
//-------------------------------------------
SHARED_Teleport( id, vOrigin[3] )
{
	// Increase so user doesn't get stuck in ground
	vOrigin[2] += 15

	// Attempt to move the user
	fm_set_user_origin( id, vOrigin )

	new iParm[4]
	iParm[0] = vOrigin[0]
	iParm[1] = vOrigin[1]
	iParm[2] = vOrigin[2]
	iParm[3] = id

	// Set up the parameters
	set_task( 0.1, "_SHARED_Teleport", TASK_TELEPORT + id, iParm, 4 )
}



//-------------------------------------------
// _SHARED_Teleport
//
//-------------------------------------------
public _SHARED_Teleport( parm[] )
{
	new id = parm[3]
	new vOrigin[3]
	
	get_user_origin( id, vOrigin )

	// User is stuck, lets teleport them back to their spawn
	if( vOrigin[2] == parm[2] )
	{
		// Find a spawn - ignore immunity and team reversal
		new iSpawnEnt = UTIL_FindFreeSpawn( id )
		
		// We can move the user yay!
		if ( iSpawnEnt > 0 )
		{
			new Float:fSpawnOrigin[3], vOrigin[3]
			
			// Get the origin of the spawn point
			pev( iSpawnEnt, pev_origin, fSpawnOrigin )

			// Convert float vector to int vector
			FVecIVec( fSpawnOrigin, vOrigin )
			
			// Move the user
			SHARED_Teleport( id, vOrigin )

			client_print( id, print_chat, "%s You are stuck! Ahhh! Moving you back to your spawn!", g_MODclient )
		}

		// We can't move the user - that sux0rsz
		else
			client_print( id, print_chat, "%s Sorry, I know you're stuck, but I can't move you right now :/", g_MODclient )
	}
}

